import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName RedisDemo
 * @Description TODO
 * @Auth ying.xiao
 * @Date 2021/6/21
 * @Version 1.0.0
 */
public class RedisDemo {
    public static void main(String[] args) {
        // Jedis连接池配置
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 最大空闲连接数, 默认8个
        jedisPoolConfig.setMaxIdle(100);
        // 最大连接数, 默认8个
        jedisPoolConfig.setMaxTotal(500);
        //最小空闲连接数, 默认0
        jedisPoolConfig.setMinIdle(0);
        // 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
        jedisPoolConfig.setMaxWaitMillis(2000); // 设置2秒
        //对拿到的connection进行validateObject校验
        jedisPoolConfig.setTestOnBorrow(true);
        Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
        jedisClusterNode.add(new HostAndPort("192.168.1.178", 7001));
        jedisClusterNode.add(new HostAndPort("192.168.1.178", 7002));
        jedisClusterNode.add(new HostAndPort("192.168.1.178", 7003));
        jedisClusterNode.add(new HostAndPort("192.168.1.178", 7004));
        jedisClusterNode.add(new HostAndPort("192.168.1.178", 7005));
        jedisClusterNode.add(new HostAndPort("192.168.1.178", 7006));
        jedisClusterNode.add(new HostAndPort("192.168.1.178", 7007));
        jedisClusterNode.add(new HostAndPort("192.168.1.178", 7008));
        JedisCluster jcd = new JedisCluster(jedisClusterNode, jedisPoolConfig);
        jcd.set("name:004","zhaoliu");
        String value = jcd.get("name:004");
        System.out.println(value);

//        //与Redis建立连接 IP+port
//         Jedis redis = new Jedis("192.168.1.178", 7001);
//        // 在Redis中写字符串 key value
//         redis.set("sex","jd-zhangfei");
//        // 获得Redis中字符串的值
//         System.out.println(redis.get("sex"));
//        // 在Redis中写list
//         redis.lpush("jedis:list:1","1","2","3","4","5");
//       //  获得list的长度
//         System.out.println(redis.llen("jedis:list:1"));

    }
}
